package com.zheng.api.server.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.impl.BaseWxPayServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.zheng.api.server.service.base.pay.*;
import com.zheng.api.server.service.channel.pay.alipay.AlipayConfig;
import com.zheng.api.server.service.channel.pay.wechat.WxPayProperties;
import com.zheng.api.server.service.mq.pay.Mq4MchPayNotify;
import com.zheng.api.server.service.mq.pay.Mq4RefundNotify;
import com.zheng.api.server.service.mq.pay.Mq4TransNotify;
import com.zheng.api.server.util.WxPayUtil;
import com.zheng.common.constant.PayConstant;
import com.zheng.common.domain.BaseParam;
import com.zheng.common.enumm.RetEnum;
import com.zheng.common.util.*;
import com.zheng.pay.dao.model.*;
import com.zheng.pay.rpc.api.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付接口
 * Created by shuzheng on 2017/2/19.
 */
@Service
public class ApiServerPayService extends BaseCommonService{

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiServerPayService.class);

    @Autowired
    PayIapReceiptService payIapReceiptService;
    @Autowired
    PayMchNotifyService payMchNotifyService;
    @Autowired
    PayPayOrderService payPayOrderService;
    @Autowired
    PayRefundOrderService payRefundOrderService;
    @Autowired
    PayTransOrderService payTransOrderService;

    @Autowired
    private AlipayConfig alipayConfig;
    @Autowired
    BaseNotify4MchPay baseNotify4MchPay;
    @Autowired
    Mq4RefundNotify mq4RefundNotify;
    @Autowired
    Mq4MchPayNotify mq4MchPayNotify;
    @Autowired
    Mq4TransNotify mq4TransNotify;

    @Autowired
    Base4PayOrderService base4PayOrderService;
    @Autowired
    Base4RefundOrderService base4RefundOrderService;
    @Resource
    private WxPayProperties wxPayProperties;

    @Autowired
    PayChannel4AliService payChannel4AliService;
    @Autowired
    PayChannel4WxService payChannel4WxService;


    public JSONObject getByMchId(String mchid) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mchid", mchid);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = null;

        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("查询商户信息失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            result = RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }else{
            String tmpmchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
            if (ObjectValidUtil.isInvalid(tmpmchid)) {
                LOGGER.warn("查询商户信息失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
                result = RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
            }else{
                PayMchInfoExample payMchInfoExample = new PayMchInfoExample();
                payMchInfoExample.createCriteria().andMchidEqualTo(tmpmchid);
                PayMchInfo payMchInfo = super.baseSelectMchInfo(tmpmchid);
                if (payMchInfo == null){
                    result = RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
                }else {
                    String jsonResult = JsonUtil.object2Json(payMchInfo);
                    result = RpcUtil.createBizResult(baseParam, jsonResult);
                }
            }
        }
        String s = RpcUtil.mkRet(result);
        if(s==null) return null;
        return JSONObject.parseObject(s);
    }

    public String doAliPayNotify(Map params) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("params", params);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = innerDoAliPayNotify(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) {
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        return s;
    }

    public String doWxPayNotify(String xmlResult) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("xmlResult", xmlResult);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        // 返回给微信的数据格式已经有service处理(包括正确与错误),肯定会返回result
        Map<String, Object> result = innerDoWxPayNotify(jsonParam);
        return RpcUtil.mkRet(result);
    }

    public JSONObject getByMchIdAndChannelId(String mchid, String channelid) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mchid", mchid);
        paramMap.put("channelid", channelid);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = innerSelectPayChannel(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) return null;
        return JSONObject.parseObject(s);
    }

    public int createPayOrder(JSONObject payOrder) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("payOrder", payOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = innerCreatePayOrder(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) return 0;
        return Integer.parseInt(s);
    }

    public JSONObject queryPayOrder(String mchid, String payorderid, String mchOrderNo, String executeNotify) {
        Map<String,Object> paramMap = new HashMap<>();
        Map<String, Object> result;
        if(org.apache.commons.lang3.StringUtils.isNotBlank(payorderid)) {
            paramMap.put("mchid", mchid);
            paramMap.put("payorderid", payorderid);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSelectBymchidAndPayOrderId(jsonParam);
        }else {
            paramMap.put("mchid", mchid);
            paramMap.put("mchOrderNo", mchOrderNo);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSelectBymchidAndMchOrderNo(jsonParam);
        }
        String s = RpcUtil.mkRet(result);
        if(s == null) return null;
        boolean isNotify = Boolean.parseBoolean(executeNotify);
        JSONObject payOrder = JSONObject.parseObject(s);
        if(isNotify) {
            paramMap = new HashMap<>();
            paramMap.put("payorderid", payorderid);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSendBizPayNotify(jsonParam);
            s = RpcUtil.mkRet(result);
            LOGGER.info("业务查单完成,并再次发送业务支付通知.发送结果:{}", s);
        }
        return payOrder;
    }

    public String doWxPayReq(String tradeType, JSONObject payOrder, String resKey) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("tradeType", tradeType);
        paramMap.put("payOrder", payOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = payChannel4WxService.doWxPayReq(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) {
            return PayCommonUtil.makeRetData(PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用微信支付失败"), resKey);
        }
        Map<String, Object> map = PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
        map.putAll((Map) result.get("bizResult"));
        return PayCommonUtil.makeRetData(map, resKey);
    }

    public String doAliPayReq(String channelid, JSONObject payOrder, String resKey) {

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("payOrder", payOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result;
        switch (channelid) {
            case PayConstant.PAY_CHANNEL_ALIPAY_MOBILE :
                result = payChannel4AliService.doAliPayMobileReq(jsonParam);
                break;
            case PayConstant.PAY_CHANNEL_ALIPAY_PC :
                result = payChannel4AliService.doAliPayPcReq(jsonParam);
                break;
            case PayConstant.PAY_CHANNEL_ALIPAY_WAP :
                result = payChannel4AliService.doAliPayWapReq(jsonParam);
                break;
            case PayConstant.PAY_CHANNEL_ALIPAY_QR :
                result = payChannel4AliService.doAliPayQrReq(jsonParam);
                break;
            default:
                result = null;
                break;
        }
        String s = RpcUtil.mkRet(result);
        if(s == null) {
            return PayCommonUtil.makeRetData(PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用支付宝支付失败"), resKey);
        }
        Map<String, Object> map = PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
        map.putAll((Map) result.get("bizResult"));
        return PayCommonUtil.makeRetData(map, resKey);
    }

    public int createRefundOrder(JSONObject refundOrder) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("refundOrder", refundOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = innerCreateRefundOrder(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) return 0;
        return Integer.parseInt(s);
    }

    public void sendRefundNotify(String refundOrderId, String channelName) {

        JSONObject object = new JSONObject();
        object.put("refundorderid", refundOrderId);
        object.put("channelname", channelName);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("msg", object);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        innerSendRefundNotify(jsonParam);
    }

    public JSONObject queryRefundOrder(String mchid, String refundOrderId, String mchRefundNo, String executeNotify) {
        Map<String,Object> paramMap = new HashMap<>();
        Map<String, Object> result;
        if(org.apache.commons.lang3.StringUtils.isNotBlank(refundOrderId)) {
            paramMap.put("mchid", mchid);
            paramMap.put("refundorderid", refundOrderId);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = selectBymchidAndRefundOrderId(jsonParam);
        }else {
            paramMap.put("mchid", mchid);
            paramMap.put("mchrefundno", mchRefundNo);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = selectBymchidAndMchRefundNo(jsonParam);
        }
        String s = RpcUtil.mkRet(result);
        if(s == null) return null;
        boolean isNotify = Boolean.parseBoolean(executeNotify);
        JSONObject payOrder = JSONObject.parseObject(s);
        if(isNotify) {
            paramMap = new HashMap<>();
            paramMap.put("refundorderid", refundOrderId);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSendBizPayNotify(jsonParam);
            s = RpcUtil.mkRet(result);
            LOGGER.info("业务查单完成,并再次发送业务支付通知.发送结果:{}", s);
        }
        return payOrder;
    }

    public String doWxRefundReq(String tradeType, JSONObject refundOrder, String resKey) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("tradeType", tradeType);
        paramMap.put("refundOrder", refundOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = payChannel4AliService.doAliRefundReq(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) {
            return PayCommonUtil.makeRetData(PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用微信支付失败"), resKey);
        }
        Map<String, Object> map = PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
        map.putAll((Map) result.get("bizResult"));
        return PayCommonUtil.makeRetData(map, resKey);
    }

    public int createTransOrder(JSONObject transOrder) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("transOrder", transOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = innerCreateTransOrder(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) return 0;
        return Integer.parseInt(s);
    }

    public void sendTransNotify(String transOrderId, String channelName) {
        JSONObject object = new JSONObject();
        object.put("transOrderId", transOrderId);
        object.put("channelname", channelName);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("msg", object);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        innerSendTransNotify(jsonParam);
    }

    public JSONObject queryTransOrder(String mchid, String transOrderId, String mchTransNo, String executeNotify) {
        Map<String,Object> paramMap = new HashMap<>();
        Map<String, Object> result;
        if(org.apache.commons.lang3.StringUtils.isNotBlank(transOrderId)) {
            paramMap.put("mchid", mchid);
            paramMap.put("transOrderId", transOrderId);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSelectBymchidAndTransOrderId(jsonParam);
        }else {
            paramMap.put("mchid", mchid);
            paramMap.put("mchTransNo", mchTransNo);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSelectBymchidAndMchTransNo(jsonParam);
        }
        String s = RpcUtil.mkRet(result);
        if(s == null) return null;
        boolean isNotify = Boolean.parseBoolean(executeNotify);
        JSONObject payOrder = JSONObject.parseObject(s);
        if(isNotify) {
            paramMap = new HashMap<>();
            paramMap.put("transOrderId", transOrderId);
            String jsonParam = RpcUtil.createBaseParam(paramMap);
            result = innerSendBizPayNotify(jsonParam);
            s = RpcUtil.mkRet(result);
            LOGGER.info("业务查单完成,并再次发送业务支付通知.发送结果:{}", s);
        }
        return payOrder;
    }

    public String doWxTransReq(String tradeType, JSONObject payOrder, String resKey) {

        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("tradeType", tradeType);
        paramMap.put("payOrder", payOrder);
        String jsonParam = RpcUtil.createBaseParam(paramMap);
        Map<String, Object> result = payChannel4WxService.doWxPayReq(jsonParam);
        String s = RpcUtil.mkRet(result);
        if(s == null) {
            return PayCommonUtil.makeRetData(PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用微信支付失败"), resKey);
        }
        Map<String, Object> map = PayCommonUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
        map.putAll((Map) result.get("bizResult"));
        return PayCommonUtil.makeRetData(map, resKey);
    }

    private Map innerSelectBymchidAndPayOrderId(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和支付订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String payorderid = baseParam.isNullValue("payorderid") ? null : bizParamMap.get("payorderid").toString();
        if (ObjectValidUtil.isInvalid(mchid, payorderid)) {
            LOGGER.warn("根据商户号和支付订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayPayOrder payPayOrder = null;
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchid);
        criteria.andPayorderidEqualTo(payorderid);
        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        payPayOrder = CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);

        if(payPayOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(payPayOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }

    private Map innerSelectBymchidAndMchOrderNo(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String mchOrderNo = baseParam.isNullValue("mchOrderNo") ? null : bizParamMap.get("mchOrderNo").toString();
        if (ObjectValidUtil.isInvalid(mchid, mchOrderNo)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayPayOrder payOrder = null;

        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchid);
        criteria.andMchordernoEqualTo(mchOrderNo);
        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        payOrder = CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);

        if(payOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(payOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }

    private Map innerSendBizPayNotify(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("发送业务支付通知失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String payorderid = baseParam.isNullValue("payorderid") ? null : bizParamMap.get("payorderid").toString();
        if(ObjectValidUtil.isInvalid(payorderid)) {
            LOGGER.warn("发送业务支付通知失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }

        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payorderid);

        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        PayPayOrder payOrder =  CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);

        if(payOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        try {
            // 发送业务支付通知
            baseNotify4MchPay.doNotify(payOrder, false);
        }catch (Exception e) {
            return RpcUtil.createBizResult(baseParam, 0);
        }
        return RpcUtil.createBizResult(baseParam, 1);
    }
    private Map innerDoAliPayNotify(String jsonParam) {
        String logPrefix = "【处理支付宝支付回调】";
        LOGGER.info("====== 开始处理支付宝支付回调通知 ======");
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("处理支付宝支付回调失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        Map params = baseParam.isNullValue("params") ? null : (Map) bizParamMap.get("params");
        if (ObjectValidUtil.isInvalid(params)) {
            LOGGER.warn("处理支付宝支付回调失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        Map<String, Object> payContext = new HashMap();
        PayPayOrder payOrder;
        payContext.put("parameters", params);
        if(!verifyAliPayParams(payContext)) {
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_PAY_NOTIFY_VERIFY_FAIL);
        }
        LOGGER.info("{}验证支付通知数据及签名通过", logPrefix);
        String trade_status = params.get("trade_status").toString();		// 交易状态
        // 支付状态成功或者完成
        if (trade_status.equals(PayConstant.AlipayConstant.TRADE_STATUS_SUCCESS) ||
                trade_status.equals(PayConstant.AlipayConstant.TRADE_STATUS_FINISHED)) {
            int updatePayOrderRows;
            payOrder = (PayPayOrder)payContext.get("payOrder");
            byte payStatus = payOrder.getStatus(); // 0：订单生成，1：支付中，-1：支付失败，2：支付成功，3：业务处理完成，-2：订单过期
            if (payStatus != PayConstant.PAY_STATUS_SUCCESS && payStatus != PayConstant.PAY_STATUS_COMPLETE) {

                String channelOrderNo = StringUtil.toString(params.get("trade_no"), null);
                PayPayOrder payOrder2 = new PayPayOrder();
                payOrder2.setPayorderid(payOrder.getPayorderid());
                payOrder2.setStatus(PayConstant.PAY_STATUS_SUCCESS);
                payOrder2.setPaysucctime(DateUtil.getDate());
                if(StringUtils.isNotBlank(channelOrderNo)) payOrder.setChannelorderno(channelOrderNo);
                PayPayOrderExample example = new PayPayOrderExample();
                PayPayOrderExample.Criteria criteria = example.createCriteria();
                criteria.andPayorderidEqualTo(payOrder.getPayorderid());
                criteria.andStatusEqualTo(PayConstant.PAY_STATUS_PAYING);
                updatePayOrderRows =  payPayOrderService.updateByExample(payOrder2, example);

                if (updatePayOrderRows != 1) {
                    LOGGER.error("{}更新支付状态失败,将payorderid={},更新payStatus={}失败", logPrefix, payOrder.getPayorderid(), PayConstant.PAY_STATUS_SUCCESS);
                    LOGGER.info("{}响应给支付宝结果：{}", logPrefix, PayConstant.RETURN_ALIPAY_VALUE_FAIL);
                    return RpcUtil.createBizResult(baseParam, PayConstant.RETURN_ALIPAY_VALUE_FAIL);
                }
                LOGGER.info("{}更新支付状态成功,将payorderid={},更新payStatus={}成功", logPrefix, payOrder.getPayorderid(), PayConstant.PAY_STATUS_SUCCESS);
                payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
            }
        }else{
            // 其他状态
            LOGGER.info("{}支付状态trade_status={},不做业务处理", logPrefix, trade_status);
            LOGGER.info("{}响应给支付宝结果：{}", logPrefix, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
            return RpcUtil.createBizResult(baseParam, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        }
        baseNotify4MchPay.doNotify(payOrder, true);
        LOGGER.info("====== 完成处理支付宝支付回调通知 ======");
        return RpcUtil.createBizResult(baseParam, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
    }

    private Map innerDoWxPayNotify(String jsonParam) {
        String logPrefix = "【处理微信支付回调】";
        LOGGER.info("====== 开始处理微信支付回调通知 ======");
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        try {
            Map<String, Object> bizParamMap = baseParam.getBizParamMap();
            if (ObjectValidUtil.isInvalid(bizParamMap)) {
                LOGGER.warn("处理微信支付回调失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
                return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail(RetEnum.RET_PARAM_NOT_FOUND.getMessage()));
                //return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
            }
            String xmlResult = baseParam.isNullValue("xmlResult") ? null : bizParamMap.get("xmlResult").toString();
            if (ObjectValidUtil.isInvalid(xmlResult)) {
                LOGGER.warn("处理微信支付回调失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
                return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail(RetEnum.RET_PARAM_INVALID.getMessage()));
                //return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
            }
            BaseWxPayServiceImpl wxPayService = new WxPayServiceImpl();
            WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlResult);
            Map<String, Object> payContext = new HashMap();
            payContext.put("parameters", result);
            // 验证业务数据是否正确,验证通过后返回PayOrder和WxPayConfig对象
            if(!verifyWxPayParams(payContext)) {
                return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail((String) payContext.get("retMsg")));
            }
            PayPayOrder payOrder = (PayPayOrder) payContext.get("payOrder");
            WxPayConfig wxPayConfig = (WxPayConfig) payContext.get("wxPayConfig");
            wxPayService.setConfig(wxPayConfig);
            // 这里做了签名校验(这里又做了一次xml转换对象,可以考虑优化)
            //wxPayService.parseOrderNotifyResult(xmlResult);
            result.checkResult(wxPayService,wxPayConfig.getSignType(),true);
            // 处理订单
            byte payStatus = payOrder.getStatus(); // 0：订单生成，1：支付中，-1：支付失败，2：支付成功，3：业务处理完成，-2：订单过期
            if (payStatus != PayConstant.PAY_STATUS_SUCCESS && payStatus != PayConstant.PAY_STATUS_COMPLETE) {

                PayPayOrder payOrder2 = new PayPayOrder();
                //payOrder2.setPayorderid(payOrder.getPayorderid());
                payOrder2.setStatus(PayConstant.PAY_STATUS_SUCCESS);
                payOrder2.setPaysucctime(DateUtil.getDate());
                payOrder2.setUpdatetime(DateUtil.getDate());
                if(StringUtils.isNotBlank(result.getTransactionId())) payOrder2.setChannelorderno(result.getTransactionId());
                PayPayOrderExample example = new PayPayOrderExample();
                PayPayOrderExample.Criteria criteria = example.createCriteria();
                criteria.andPayorderidEqualTo(payOrder.getPayorderid());
                criteria.andStatusEqualTo(PayConstant.PAY_STATUS_PAYING);
                int updatePayOrderRows = payPayOrderService.updateByExampleSelective(payOrder2, example);

                if (updatePayOrderRows != 1) {
                    LOGGER.error("{}更新支付状态失败,将payorderid={},更新payStatus={}失败", logPrefix, payOrder.getPayorderid(), PayConstant.PAY_STATUS_SUCCESS);
                    return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail("处理订单失败"));
                }
                LOGGER.error("{}更新支付状态成功,将payorderid={},更新payStatus={}成功", logPrefix, payOrder.getPayorderid(), PayConstant.PAY_STATUS_SUCCESS);
                payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
            }
            // 业务系统后端通知
            baseNotify4MchPay.doNotify(payOrder, true);
            LOGGER.info("====== 完成处理微信支付回调通知 ======");
            return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.success("OK"));
        } catch (WxPayException e) {
            //出现业务错误
            LOGGER.error( "微信回调结果异常,异常原因",e);
            LOGGER.info("{}请求数据result_code=FAIL", logPrefix);
            LOGGER.info("err_code:", e.getErrCode());
            LOGGER.info("err_code_des:", e.getErrCodeDes());
            return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail(e.getMessage()));
        } catch (Exception e) {
            LOGGER.error( "微信回调结果异常,异常原因",e);
            return RpcUtil.createBizResult(baseParam, WxPayNotifyResponse.fail(e.getMessage()));
        }
    }

    private Map innerSelectPayChannel(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("查询支付渠道信息失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String channelid = baseParam.isNullValue("channelid") ? null : bizParamMap.get("channelid").toString();
        if (ObjectValidUtil.isInvalid(mchid, channelid)) {
            LOGGER.warn("查询支付渠道信息失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayPayChannel payChannel = super.baseSelectPayChannel(mchid,channelid);

        if(payChannel == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(payChannel);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }

    public Map innerCreatePayOrder(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("新增支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        JSONObject payOrderObj = baseParam.isNullValue("payOrder") ? null : JSONObject.parseObject(bizParamMap.get("payOrder").toString());
        if(payOrderObj == null) {
            LOGGER.warn("新增支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayPayOrder payOrder = BeanConvertUtils.map2Bean(payOrderObj, PayPayOrder.class);
        if(payOrder == null) {
            LOGGER.warn("新增支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        int result = payPayOrderService.insertSelective(payOrder);

        LOGGER.warn("支付中心保存支付订单结果, {}. jsonParam={}", result, jsonParam);

        return RpcUtil.createBizResult(baseParam, result);
    }
    /**
     * 验证支付宝支付通知参数
     * @return
     */
    private boolean verifyAliPayParams(Map<String, Object> payContext) {
        Map<String,String> params = (Map<String,String>)payContext.get("parameters");
        String out_trade_no = params.get("out_trade_no");		// 商户订单号
        String total_amount = params.get("total_amount"); 		// 支付金额
        if (StringUtils.isEmpty(out_trade_no)) {
            LOGGER.error("AliPay Notify parameter out_trade_no is empty. out_trade_no={}", out_trade_no);
            payContext.put("retMsg", "out_trade_no is empty");
            return false;
        }
        if (StringUtils.isEmpty(total_amount)) {
            LOGGER.error("AliPay Notify parameter total_amount is empty. total_fee={}", total_amount);
            payContext.put("retMsg", "total_amount is empty");
            return false;
        }
        String errorMessage;
        // 查询payOrder记录
        String payorderid = out_trade_no;

        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payorderid);

        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        PayPayOrder payOrder =  CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);

        if (payOrder == null) {
            LOGGER.error("Can't found payOrder form db. payorderid={}, ", payorderid);
            payContext.put("retMsg", "Can't found payOrder");
            return false;
        }
        // 查询payChannel记录
        String mchid = payOrder.getMchid();
        String channelid = payOrder.getChannelid();


        PayPayChannel payChannel = super.baseSelectPayChannel(mchid,channelid);

        if(payChannel == null) {
            LOGGER.error("Can't found payChannel form db. mchid={} channelid={}, ", payorderid, mchid, channelid);
            payContext.put("retMsg", "Can't found payChannel");
            return false;
        }
        boolean verify_result = false;
        try {
            verify_result = AlipaySignature.rsaCheckV1(params, alipayConfig.init(payChannel.getParam()).getAlipay_public_key(), AlipayConfig.CHARSET, "RSA2");
        } catch (AlipayApiException e) {
            LOGGER.error("AlipaySignature.rsaCheckV1 error",e);
        }

        // 验证签名
        if (!verify_result) {
            errorMessage = "rsaCheckV1 failed.";
            LOGGER.error("AliPay Notify parameter {}", errorMessage);
            payContext.put("retMsg", errorMessage);
            return false;
        }

        // 核对金额
        long aliPayAmt = new BigDecimal(total_amount).movePointRight(2).longValue();
        long dbPayAmt = payOrder.getAmount().longValue();
        if (dbPayAmt != aliPayAmt) {
            LOGGER.error("db payOrder record payPrice not equals total_amount. total_amount={},payorderid={}", total_amount, payorderid);
            payContext.put("retMsg", "");
            return false;
        }
        payContext.put("payOrder", payOrder);
        return true;
    }

    /**
     * 验证微信支付通知参数
     * @return
     */
    private boolean verifyWxPayParams(Map<String, Object> payContext) {
        WxPayOrderNotifyResult params = (WxPayOrderNotifyResult)payContext.get("parameters");

        //校验结果是否成功
        if (!PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(params.getResultCode())
                || !PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(params.getResultCode())) {
            LOGGER.error("returnCode={},resultCode={},errCode={},errCodeDes={}", params.getReturnCode(), params.getResultCode(), params.getErrCode(), params.getErrCodeDes());
            payContext.put("retMsg", "notify data failed");
            return false;
        }

        Integer total_fee = params.getTotalFee();   			// 总金额
        String out_trade_no = params.getOutTradeNo();			// 商户系统订单号

        // 查询payOrder记录
        String payorderid = out_trade_no;

        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payorderid);

        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        PayPayOrder payOrder =  CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);

        if (payOrder==null) {
            LOGGER.error("Can't found payOrder form db. payorderid={}, ", payorderid);
            payContext.put("retMsg", "Can't found payOrder");
            return false;
        }

        // 查询payChannel记录
        String mchid = payOrder.getMchid();
        String channelid = payOrder.getChannelid();
        PayPayChannel payChannel = super.baseSelectPayChannel(mchid,channelid);

        if(payChannel == null) {
            LOGGER.error("Can't found payChannel form db. mchid={} channelid={}, ", payorderid, mchid, channelid);
            payContext.put("retMsg", "Can't found payChannel");
            return false;
        }
        payContext.put("wxPayConfig", WxPayUtil.getWxPayConfig(payChannel.getParam()));

        // 核对金额
        long wxPayAmt = new BigDecimal(total_fee).longValue();
        long dbPayAmt = payOrder.getAmount().longValue();
        if (dbPayAmt != wxPayAmt) {
            LOGGER.error("db payOrder record payPrice not equals total_fee. total_fee={},payorderid={}", total_fee,payorderid);
            payContext.put("retMsg", "total_fee is not the same");
            return false;
        }

        payContext.put("payOrder", payOrder);
        return true;
    }

    private Map innerCreateRefundOrder(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("新增退款订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        JSONObject refundOrderObj = baseParam.isNullValue("refundOrder") ? null : JSONObject.parseObject(bizParamMap.get("refundOrder").toString());
        if(refundOrderObj == null) {
            LOGGER.warn("新增退款订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayRefundOrder refundOrder = BeanConvertUtils.map2Bean(refundOrderObj, PayRefundOrder.class);
        if(refundOrder == null) {
            LOGGER.warn("新增退款订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        int result = payRefundOrderService.insertSelective(refundOrder);
        return RpcUtil.createBizResult(baseParam, result);
    }

    private Map innerSendRefundNotify(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("发送退款订单处理失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String msg = baseParam.isNullValue("msg") ? null : bizParamMap.get("msg").toString();
        if (ObjectValidUtil.isInvalid(msg)) {
            LOGGER.warn("发送退款订单处理失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        int result = 1;
        try {
            mq4RefundNotify.send(msg);
        }catch (Exception e) {
            LOGGER.error("",e);
            result = 0;
        }
        return RpcUtil.createBizResult(baseParam, result);
    }

    private Map selectBymchidAndRefundOrderId(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和退款订单号查询退款订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String refundOrderId = baseParam.isNullValue("refundorderid") ? null : bizParamMap.get("refundorderid").toString();
        if (ObjectValidUtil.isInvalid(mchid, refundOrderId)) {
            LOGGER.warn("根据商户号和退款订单号查询退款订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayRefundOrder refundOrder = base4RefundOrderService.baseSelectByMchIdAndRefundOrderId(mchid, refundOrderId);
        if(refundOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(refundOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }
    private Map selectBymchidAndMchRefundNo(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String mchRefundNo = baseParam.isNullValue("mchRefundNo") ? null : bizParamMap.get("mchRefundNo").toString();
        if (ObjectValidUtil.isInvalid(mchid, mchRefundNo)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }

        PayRefundOrderExample example = new PayRefundOrderExample();
        PayRefundOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchid);
        criteria.andMchrefundnoEqualTo(mchRefundNo);
        List<PayRefundOrder> refundOrderList = payRefundOrderService.selectByExample(example);
        PayRefundOrder refundOrder =  CollectionUtils.isEmpty(refundOrderList) ? null : refundOrderList.get(0);


        if(refundOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(refundOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }

    private Map innerCreateTransOrder(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("新增转账订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        JSONObject transOrderObj = baseParam.isNullValue("transOrder") ? null : JSONObject.parseObject(bizParamMap.get("transOrder").toString());
        if(transOrderObj == null) {
            LOGGER.warn("新增转账订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayTransOrder transOrder = BeanConvertUtils.map2Bean(transOrderObj, PayTransOrder.class);
        if(transOrder == null) {
            LOGGER.warn("新增转账订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        int result = payTransOrderService.insertSelective(transOrder);;
        return RpcUtil.createBizResult(baseParam, result);
    }
    private Map innerSendTransNotify(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("发送转账订单处理失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String msg = baseParam.isNullValue("msg") ? null : bizParamMap.get("msg").toString();
        if (ObjectValidUtil.isInvalid(msg)) {
            LOGGER.warn("发送转账订单处理失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        int result = 1;
        try {
            mq4TransNotify.send(msg);
        }catch (Exception e) {
            LOGGER.error("",e);
            result = 0;
        }
        return RpcUtil.createBizResult(baseParam, result);
    }

    private Map innerSelectBymchidAndTransOrderId(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和转账订单号查询转账订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String transOrderId = baseParam.isNullValue("transOrderId") ? null : bizParamMap.get("transOrderId").toString();
        if (ObjectValidUtil.isInvalid(mchid, transOrderId)) {
            LOGGER.warn("根据商户号和转账订单号查询转账订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchid);
        criteria.andTransorderidEqualTo(transOrderId);
        List<PayTransOrder> transOrderList = payTransOrderService.selectByExample(example);
        PayTransOrder transOrder =  CollectionUtils.isEmpty(transOrderList) ? null : transOrderList.get(0);

        if(transOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(transOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }

    public Map innerSelectBymchidAndMchTransNo(String jsonParam) {
        BaseParam baseParam = JsonUtil.getObjectFromJson(jsonParam, BaseParam.class);
        Map<String, Object> bizParamMap = baseParam.getBizParamMap();
        if (ObjectValidUtil.isInvalid(bizParamMap)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_NOT_FOUND.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
        }
        String mchid = baseParam.isNullValue("mchid") ? null : bizParamMap.get("mchid").toString();
        String mchTransNo = baseParam.isNullValue("mchTransNo") ? null : bizParamMap.get("mchTransNo").toString();
        if (ObjectValidUtil.isInvalid(mchid, mchTransNo)) {
            LOGGER.warn("根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}", RetEnum.RET_PARAM_INVALID.getMessage(), jsonParam);
            return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
        }

        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchid);
        criteria.andMchtransnoEqualTo(mchTransNo);
        List<PayTransOrder> transOrderList = payTransOrderService.selectByExample(example);
        PayTransOrder transOrder = CollectionUtils.isEmpty(transOrderList) ? null : transOrderList.get(0);

        if(transOrder == null) return RpcUtil.createFailResult(baseParam, RetEnum.RET_BIZ_DATA_NOT_EXISTS);
        String jsonResult = JsonUtil.object2Json(transOrder);
        return RpcUtil.createBizResult(baseParam, jsonResult);
    }
}
